<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notepad</title>

    <!-- bootstrap -->
    <link rel="stylesheet" href="./assets/css/bootstrap.min.css" />

    <!-- MUI -->
    <link rel="stylesheet" href="./assets/plugins/mui/css/mui.min.css" />

    <!-- animate.css -->
    <link rel="stylesheet" href="./assets/css/animate.min.css" />

    <!-- 引入bootstrapValidator表单验证插件 -->
    <link rel="stylesheet" href="./assets/plugins/bootstrap-validator/bootstrapValidator.min.css" />

    <!-- 城市插件 -->
    <link rel="stylesheet" href="./assets/plugins/city-picker/city-picker.css" />

    <!-- 重置样式 -->
    <link rel="stylesheet" href="./assets/css/reset.css" />

    <!-- 公共样式 -->
    <link rel="stylesheet" href="./assets/css/common.css" />

    <!-- 页面独立的 -->
    <link rel="stylesheet" href="./assets/css/UserProfile.css" />

    <!-- jquery  -->
    <script src="./assets/js/jquery.min.js"></script>

    <!-- 引入bootstrapValidator表单验证插件 -->
    <script src="./assets/plugins/bootstrap-validator/bootstrapValidator.min.js"></script>
</head>
<body>
    <!-- 用户信息 -->
    <div class="user">
        <div class="avatar wow fadeInUp">
            <img src="./assets/images/avatar.png" />
        </div>
    </div>

    <form id="profile" class="profile wow fadeInUp" data-wow-delay="200ms">
        <div class="form-group">
            <label for="nickname">姓名</label>
            <input type="text" class="form-control" id="nickname" name="nickname" placeholder="请输入姓名" required />
        </div>

        <div class="form-group">
            <label for="mobile">手机号码</label>
            <input type="text" class="form-control" id="mobile" name="mobile" placeholder="请输入手机号码" required />
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码" />
        </div>

        <div class="form-group">
            <label for="gender">性别</label>
            <select name="gender" id="gender" class="form-control">
                <option>请选择</option>
                <option value="0">保密</option>
                <option value="1">男</option>
                <option value="2">女</option>
            </select>
        </div>

        <div class="form-group" style="position: relative;">
            <label for="region">城市</label>
            <input id="region" type="text" name="region" readonly placeholder="请选择城市" />
        </div>

        <div class="form-group">
            <label for="address">详细地址</label>
            <input type="text" class="form-control" name="address" id="address" placeholder="请输入详细地址" />
        </div>

        <div class="form-group" style="display: none;">
            <label for="avatar">更换头像</label>
            <input type="file" class="form-control" id="avatar" name="avatar" />
        </div>
        
        <div class="action">
            <button type="submit" name="submit" class="btn">提交</button>
        </div>
    </form>
</body>
</html>
<!-- wow.js -->
<script src="./assets/js/wow.min.js"></script>

<!-- bootstrap -->
<script src="./assets/js/bootstrap.min.js"></script>

<!-- MUI -->
<script src="./assets/plugins/mui/js/mui.min.js"></script>

<!-- axios请求 -->
<script src="./assets/plugins/axios/axios.min.js"></script>
<script src="./assets/plugins/axios/request.js"></script>

<!-- cookie -->
<script src="./assets/plugins/cookie.js"></script>

<!-- 判断是否登录 -->
<script src="./assets/plugins/login.js"></script>

<!-- 城市插件 -->
<script src="./assets/plugins/city-picker/city-picker.data.min.js"></script>
<script src="./assets/plugins/city-picker/city-picker.min.js"></script>

<script>
    //实例化wow.js
    new WOW().init()

    //初始化赋值
	$("#region").citypicker();

    //获取cookie信息
    var business = cookie.get('business') ? JSON.parse(cookie.get('business')) : {};

    if(business)
    {
        $(".avatar img").attr('src', business.avatar)
        $('#nickname').val(business.nickname)
        $('#mobile').val(business.mobile)
        $("#gender").val(business.gender)
        $("#address").val(business.address)

        if(business.region)
        {
            //重置
            $('#region').citypicker('reset')
            $('#region').citypicker('destroy')
            $('#region').citypicker({
                province: business.province,
                city: business.city,
                district: business.district
            })
        }
    }

    //选择头像
    $('.avatar').click(function(){
        $('#avatar').click()
    })

    //给Input元素绑定一个改变的事件
    $('#avatar').change(function(){
        //将拿到的图片，转换为本地的图片结构，进行显示
        // console.log($(this)[0].files[0])

        //选择的图片
        var file = $(this)[0].files[0]

        //创建一个文件读取器对象
        var reader = new FileReader()

        //让这个对象去读取上面的文件
        reader.readAsDataURL(file)

        //绑定一个事件，读取成功后拿到结果的事件
        reader.onload = function(ev)
        {
            //把base64的数据 渲染上去
            $(".avatar img").attr('src', ev.target.result)
        }
    })

    //表单验证
    $('#profile').bootstrapValidator({
        // 所有字段的默认错误消息
        message: '表单内容不能为空',
        //字段校验图标。默认bootstrap图标
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        // 验证字段
        fields: {
            mobile: {
                message: '手机号码验证失败',
                validators: {
                    notEmpty: {
                        message: '手机号码不能为空'
                    },
                    stringLength: {
                        min: 11,
                        max: 11,
                        message: '手机号码长度为11位'
                    },
                    regexp: {
                        regexp: /^1[3,4,5,6,7,8,9][0-9]{9}$/,
                        message: '手机号码格式有误'
                    }
                }
            },
            nickname: {
                validators: {
                    notEmpty: {
                        message: '请输入昵称'
                    },
                }
            },
        }
    }).on('success.form.bv', function(e){
        //阻止多次提交
        e.preventDefault()
    })

    $('#profile').on('submit', async function(e){
        e.preventDefault()
        //通过表单验证后再提交
        if(!$("form").data('bootstrapValidator').isValid())
        {
            //重置表单
            $('form').data('bootstrapValidator').resetForm();
            return false
        }

        //组装数据
        var data = {
            id: business.id,
            mobile: $.trim($('#mobile').val()),
            nickname: $.trim($('#nickname').val()),
            gender: $.trim($('#gender').val()),
            address: $.trim($('#address').val()),
        }

        var password = $.trim($('#password').val())

        if(password)
        {
            data.password = password
        }

        var citypicker = $('#region').data('citypicker')
        var region = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province")

        if(region)
        {
            data.region = region
        }

        // 判断是否有图片上传
        if($('#avatar')[0].files[0])
        {
            data.avatar = $('#avatar')[0].files[0]
        }

        var result = await UPLOAD({
            url: '/business/profile',
            params: data
        })

        if(result.code == 1)
        {
            mui.toast(result.msg, {duration: 1000})

            //存放cookie
            cookie.set('business', JSON.stringify(result.data))

            setTimeout(() => {
                location.reload()
            }, 1000)
        }else
        {
            //重置表单
            $('form').data('bootstrapValidator').resetForm()
            mui.toast(result.msg)
            return false
        }
    })
</script>